package com.portal.flowable.listener;

import com.google.common.collect.ImmutableSet;
import com.portal.flowable.service.IFlowBizService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.engine.delegate.event.FlowableCancelledEvent;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;

/**
 * 流程实例事件监听器：流程创建、取消、完成
 */
@Slf4j
@Component
public class BpmProcessInstanceEventListener extends AbstractFlowableEngineEventListener {

    @Resource
    private IFlowBizService iFlowBizService;

    public static final Set<FlowableEngineEventType> PROCESS_INSTANCE_EVENTS = ImmutableSet.<FlowableEngineEventType>builder()
            .add(FlowableEngineEventType.PROCESS_CREATED)
            .add(FlowableEngineEventType.PROCESS_COMPLETED)
            .add(FlowableEngineEventType.PROCESS_STARTED)
            .add(FlowableEngineEventType.PROCESS_CANCELLED)
            .build();

    public BpmProcessInstanceEventListener() {
        super(PROCESS_INSTANCE_EVENTS);
    }

    @Override
    protected void processCreated(FlowableEngineEntityEvent event) {
        //  1、TODO 保存流程启动之后的额外需要保存的数据
        ProcessInstance processInstance = (ProcessInstance) event.getEntity();
        log.info("BpmProcessInstanceEventListener====>{}", processInstance.getProcessInstanceId());
//        super.processCreated(event);
    }

    @Override
    protected void processCancelled(FlowableCancelledEvent event) {
        //  2、TODO 保存流程取消之后的额外需要保存的数据
//        super.processCancelled(event);
    }
}
